<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="800px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="内容标题" prop="title">
        <el-input v-model="formData.title" placeholder="请输入内容标题" />
      </el-form-item>
      <el-form-item label="内容类型" prop="contentType">
        <el-select v-model="formData.contentType" placeholder="请选择内容类型" clearable>
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CONTENT_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="内容正文" prop="content">
        <Editor v-model="formData.content" height="150px" />
      </el-form-item>
      <el-form-item label="内容摘要" prop="summary">
        <el-input v-model="formData.summary" placeholder="请输入内容摘要" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :value="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number
          v-model="formData.sort"
          :min="0"
          controls-position="right"
          placeholder="请输入排序"
          class="!w-full"
        />
      </el-form-item>
      <el-form-item label="是否置顶" prop="isTop">
        <el-radio-group v-model="formData.isTop">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :value="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- <el-form-item label="浏览次数" prop="viewCount">
        <el-input-number
          v-model="formData.viewCount"
          :min="0"
          controls-position="right"
          placeholder="请输入浏览次数"
          class="!w-full"
        />
      </el-form-item>
      <el-form-item label="版本号" prop="version">
        <el-input v-model="formData.version" placeholder="请输入版本号，如：v1.0" />
      </el-form-item>
      <el-form-item label="生效时间" prop="effectiveDate">
        <el-date-picker
          v-model="formData.effectiveDate"
          type="date"
          value-format="x"
          placeholder="选择生效时间"
        />
      </el-form-item>
      <el-form-item label="失效时间" prop="expiryDate">
        <el-date-picker
          v-model="formData.expiryDate"
          type="date"
          value-format="x"
          placeholder="选择失效时间"
        />
      </el-form-item> -->
      <!-- <el-form-item label="标签" prop="tags">
        <el-input v-model="formData.tags" placeholder="多个标签用逗号分隔" />
      </el-form-item> -->
      <!-- <el-form-item label="附件URL" prop="attachmentUrl">
        <el-input v-model="formData.attachmentUrl" placeholder="请输入附件URL" />
      </el-form-item>
      <el-form-item label="SEO标题" prop="seoTitle">
        <el-input v-model="formData.seoTitle" placeholder="请输入SEO标题" />
      </el-form-item>
      <el-form-item label="SEO关键词" prop="seoKeywords">
        <el-input v-model="formData.seoKeywords" placeholder="请输入SEO关键词" />
      </el-form-item>
      <el-form-item label="SEO描述" prop="seoDescription">
        <el-input
          v-model="formData.seoDescription"
          type="textarea"
          :rows="3"
          placeholder="请输入SEO描述"
        />
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ManagementApi, ManagementVO } from '@/api/system/systemConfig/management'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

/** 内容管理 表单 */
defineOptions({ name: 'ManagementForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  title: undefined,
  contentType: undefined,
  content: undefined,
  summary: undefined,
  status: undefined,
  sort: undefined,
  isTop: undefined,
  viewCount: undefined,
  version: undefined,
  effectiveDate: undefined,
  expiryDate: undefined,
  tags: undefined,
  attachmentUrl: undefined,
  seoTitle: undefined,
  seoKeywords: undefined,
  seoDescription: undefined
})
const formRules = reactive({
  title: [{ required: true, message: '内容标题不能为空', trigger: 'blur' }],
  contentType: [{ required: true, message: '内容类型不能为空', trigger: 'change' }],
  content: [{ required: true, message: '内容正文不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
  sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
  isTop: [{ required: true, message: '是否置顶不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ManagementApi.getManagement(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ManagementVO
    if (formType.value === 'create') {
      await ManagementApi.createManagement(data)
      message.success(t('common.createSuccess'))
    } else {
      await ManagementApi.updateManagement(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    title: undefined,
    contentType: undefined,
    content: undefined,
    summary: undefined,
    status: undefined,
    sort: undefined,
    isTop: undefined,
    viewCount: undefined,
    version: undefined,
    effectiveDate: undefined,
    expiryDate: undefined,
    tags: undefined,
    attachmentUrl: undefined,
    seoTitle: undefined,
    seoKeywords: undefined,
    seoDescription: undefined
  }
  formRef.value?.resetFields()
}
</script>
